package com.edooteach.api.web.controller.book;

import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.JsonUtils;
import com.edoomath.entity.course.Course;
import com.edoomath.entity.course.CourseMaterail;
import com.edoomath.entity.course.CourseUnit;
import com.edoomath.entity.course.CourseUnitHomework;
import com.edoomath.entity.course.MaterialType;
import com.edoomath.entity.course.UserCourse;
import com.edooteach.api.entity.SigninRequest;
import com.edooteach.api.servcice.CourseService;
import com.edooteach.api.servcice.UserCourseService;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/products")
public class CourseApiController {
    
    private static final Logger logger = LoggerFactory.getLogger(CourseApiController.class);
    
    @Autowired
    private CourseService courseService;
    
    @Autowired
    private UserCourseService userCourseService;

    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult list() {
      
      HttpResult httpResult = new HttpResult();
      
      List<Course> courses = courseService.loadAll(Course.class);
      
      return httpResult.success()
                       .data("courses", courses)
                       .data("selft", 10)
                       .message("获取所有书籍成功");
    }
    
    /**
     * 
     * @param productId
     * @return
     */
    @RequestMapping(value = "/{productId}/units", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult unit(@PathVariable("productId") Long productId, @RequestParam("unionid") String unionid) {
        
        HttpResult httpResult = new HttpResult();
        
        UserCourse userCourse = userCourseService.load(productId, unionid);
        if(userCourse == null) {
            httpResult.data("hasCourse", false);
        } else {
            httpResult.data("hasCourse", true);
        }
        
        Course course = courseService.load(Course.class, productId);
        List<CourseUnit> courseUnits = courseService.listUnit(productId);
        
        List<CourseMaterail> materails = courseService.listMaterialByType(productId, MaterialType.BOOK_ANSWER);
        Map<String, CourseMaterail> materailsMap = CollectionUtils.constructMapValue(materails, "courseUnit.id");
        
        return httpResult.success()
                         .data("course", course)
                         .data("courseUnits", courseUnits)
                         .data("materails", materailsMap)
                         .data("messsage1", "暂未权限访问")
                         .data("message2", "你暂未开通该课程资料访问权限，请联系管理员进行开通。");
    }
    
    /**
     * 
     * @param productId
     * @return
     */
    @RequestMapping(value = "/{productId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult detail(@PathVariable("productId") Long productId) {
        
        HttpResult httpResult = new HttpResult();
        
        Course course = courseService.load(Course.class, productId);
        List<CourseMaterail> materials = courseService.listMaterial(productId);
        //Map<String, List<CourseMaterail>> mapMaterials = CollectionUtils.constructMapValues(materials, "courseUnit.id");
        
        return httpResult.success()
                         .data("course", course)
                         .data("materials", materials)
                         .message("获取所有书籍成功");
        
    }
    
    /**
     * 
     * @param productId
     * @return
     */
    @RequestMapping(value = "/{productId}/units/{unitId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult unitDetail(@PathVariable("productId") Long productId,
                                 @PathVariable("unitId") Long unitId) {
        
        HttpResult httpResult = new HttpResult();
        
        Course course = courseService.load(Course.class, productId);
        CourseUnit courseUnit = courseService.loadUnit(unitId);
        
        CourseMaterail answer = courseService.loadByUnitType(unitId, MaterialType.BOOK_ANSWER);
        CourseMaterail exam = courseService.loadByUnitType(unitId, MaterialType.BOOK_EXAME);
        
        List<CourseMaterail> materials = courseService.listMaterialByUnitType(unitId, MaterialType.BOOK_COACH);
        
        return httpResult.success()
                         .data("course", course)
                         .data("materials", materials)
                         .data("answer", answer)
                         .data("exam", exam)
                         .data("courseUnit", courseUnit)
                         .message("获取所有书籍成功");
        
    }
    
    /**
     * 
     * @param id
     * @param requestBody
     * @return
     */
    @RequestMapping(value = "/homeworks", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult homeworks(@RequestParam(value = "unionid") String unionid,
                                @RequestParam(value = "unitId") Long unitId) {
        
        HttpResult httpResult = new HttpResult();
        
        List<CourseUnitHomework> homeworks = userCourseService.listHomework(unitId, unionid);
        
        return httpResult.success()
                         .data("homeworks", homeworks)
                         .message("上传成功");
    }
    
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/homeworks/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult homeworks(@PathVariable(value = "id") Long id) {
        
        HttpResult httpResult = new HttpResult();
        
        CourseUnitHomework homework = userCourseService.load(id);
        
        return httpResult.success()
                         .data("homework", homework)
                         .message("上传成功");
    }
        
    /**
     * 
     * @param id
     * @param requestBody
     * @return
     */
    @RequestMapping(value = "/homework", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult doHomework(@RequestParam(value = "unionid") String unionid,
            @RequestParam(value = "unitId") Long unitId,
                                 @RequestBody String requestBody) {
        
        HttpResult httpResult = new HttpResult();
        logger.info("signTask with request body [{}]", requestBody);
        
        SigninRequest signinRequest = JsonUtils.parse(requestBody, SigninRequest.class);
        logger.info("signTask with signinRequest body [{}]", JsonUtils.toJson(signinRequest));
        
        userCourseService.uploadHomework(unitId, unionid, signinRequest);
        
        return httpResult.success().message("上传成功");
    }

}
